/*
 * Copyright (c) 2011 Imre Fazekas.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the name of the Brillien nor the names of its
 * terms and concepts may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.vii.brillien.core.component.sso;

import com.vii.brillien.core.component.SuperPresence;
import com.vii.brillien.kernel.BrillienException;
import com.vii.brillien.kernel.annotations.PresenceService;
import com.vii.brillien.kernel.annotations.lifecycle.Resident;
import com.vii.brillien.kernel.axiom.sso.Session;
import com.vii.brillien.kernel.axiom.sso.SubjectService;
import com.vii.streamline.annotation.P;

import java.util.HashMap;
import java.util.List;

/**
 * Reference SubjectService implementation as a Presence
 */
@PresenceService( logLevel = PresenceService.FINE )
@Resident
public abstract class SubjectPresence<R> extends SuperPresence<R> implements SubjectService {

    protected HashMap<String, String> configuration;

    {
        configuration = new HashMap<String, String>();
    }

    public HashMap<String, String> getConfiguration() {
        return configuration;
    }

    public void setConfiguration(HashMap<String, String> configuration) throws BrillienException {
        this.configuration = configuration;
        initiate();
    }

    protected abstract void initiate() throws BrillienException;

    protected abstract Session innerAuthenticate(String username, String principal) throws BrillienException;

    protected abstract void    innerLogout( Session session ) throws BrillienException;

    protected abstract boolean innerIsAuthenticated(Session session) throws BrillienException;

    protected abstract boolean innerHasRole(Session session, String roleIdentifier) throws BrillienException;

    protected abstract boolean innerHasARole(Session session, List<String> roleIdentifiers) throws BrillienException;

    protected abstract boolean innerHasRoles(Session session, List<String> roleIdentifiers) throws BrillienException;

    protected abstract boolean innerHasPermission(Session session, String permission) throws BrillienException;

    protected abstract boolean innerHasAPermission(Session session, List<String> permissions) throws BrillienException;

    protected abstract boolean innerHasPermissions(Session session, List<String> permissions) throws BrillienException;

    @Override
    public Session authenticate( @P(name="username")String username, @P(name="principal")String principal ) throws BrillienException{
        Session s = innerAuthenticate( username, principal );

        log( s + " session built up for: " + username );

        return s;
    }

    @Override
    public void logout( @P(name="session")Session session ) throws BrillienException{
        innerLogout( session );

        log( session + " logged out." );
    }

    @Override
    public boolean isAuthenticated(@P(name="session")Session session) throws BrillienException{
        boolean b = innerIsAuthenticated( session );

        log( session + " authenticated " + (b?"Successfully":"Unsuccessfully") );

        return b;
    }

    @Override
    public boolean hasRole(@P(name="session")Session session, @P(name="roleIdentifier")String roleIdentifier) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasRole( session, roleIdentifier );

        log( (b?"Successful":"Unsuccessful") + " role checking for: " + session + ":" + roleIdentifier );

        return b;
    }

    @Override
    public boolean hasARole(@P(name="session")Session session, @P(name="roleIdentifiers")List<String> roleIdentifiers) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasARole( session, roleIdentifiers );

        log( (b?"Successful":"Unsuccessful") + " role checking for: " + session + ":" + roleIdentifiers );

        return b;
    }

    @Override
    public boolean hasRoles(@P(name="session")Session session, @P(name="roleIdentifiers")List<String> roleIdentifiers) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasRoles( session, roleIdentifiers );

        log( (b?"Successful":"Unsuccessful") + " role checking for: " + session + ":" + roleIdentifiers );

        return b;
    }

    @Override
    public boolean hasPermission(@P(name="session")Session session, @P(name="permission")String permission) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasPermission( session, permission );

        log( (b?"Successful":"Unsuccessful") + " permission checking for: " + session + ":" + permission );

        return b;
    }

    @Override
    public boolean hasAPermission(@P(name="session")Session session, @P(name="permissions")List<String> permissions) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasAPermission( session, permissions );

        log( (b?"Successful":"Unsuccessful") + " permission checking for: " + session + ":" + permissions );

        return b;
    }

    @Override
    public boolean hasPermissions(@P(name="session")Session session, @P(name="permissions")List<String> permissions) throws BrillienException {
        boolean b = isAuthenticated( session ) && innerHasPermissions( session, permissions );

        log( (b?"Successful":"Unsuccessful") + " permission checking for: " + session + ":" + permissions );

        return b;
    }

}

